package com.boot.ys.mybatisplus.Test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 读取表名和表注释
 *
 * @author: 杨升
 * @date: 2025-05-17 23:43
 * @Version 1.0
 */

public class ReadMysql {
    public static void main(String[] args) {
        String filePath = "C:\\Users\\Acer\\Desktop\\1\\springboot6b9p2i50\\db\\springboot6b9p2i50.sql";
        List<TableInfo> tables = parseSqlFile(filePath);

        System.out.println("==========================================表信息-开始==========================================");
        tables.forEach(System.out::println);

        System.out.println("==========================================表信息-结束==========================================");


        // 帮我循环装到这个map里面，key是表名，value是注释
        System.out.println();

        System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        System.out.println("Map<String, String> replacementMap = new HashMap<>();");
        for (TableInfo table : tables) {
            System.out.printf(
                    "replacementMap.put(\"%s\", \"%s\");\n",  // 移除了%-20s的宽度格式控制
                    table.tableName.trim(),            // 加上trim()确保去除首尾空格
                    table.comment.isEmpty() ? "无注释" : table.comment
            );
        }
        System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");


    }


    // 存储结果的POJO
    static class TableInfo {
        String tableName;
        String comment;

        public TableInfo(String tableName, String comment) {
            this.tableName = tableName;
            this.comment = comment;
        }

        @Override
        public String toString() {
            return String.format("表名：%-20s 注释：%s", tableName, comment);
        }
    }


    private static List<TableInfo> parseSqlFile(String filePath) {
        List<TableInfo> result = new ArrayList<>();
        Pattern tablePattern = Pattern.compile(
                "(?i)CREATE TABLE `?(\\w+)`?.*?COMMENT\\s*=\\s*'([^']*)'",
                Pattern.DOTALL | Pattern.CASE_INSENSITIVE
        );

        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            StringBuilder sb = new StringBuilder();
            String line;
            boolean inCreateTable = false;

            while ((line = br.readLine()) != null) {
                // 合并多行CREATE TABLE语句
                if (line.trim().toUpperCase().startsWith("CREATE TABLE")) {
                    inCreateTable = true;
                    sb.setLength(0);
                }

                if (inCreateTable) {
                    sb.append(line.replace("`", "")); // 去除反引号
                    if (line.contains(";")) {
                        inCreateTable = false;
                        processTableDefinition(sb.toString(), tablePattern, result);
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("读取文件失败请检查文件是否存在或路径是否正确");
            e.printStackTrace();
        }
        return result;
    }

    private static void processTableDefinition(String sql,
                                               Pattern pattern,
                                               List<TableInfo> result) {
        Matcher matcher = pattern.matcher(sql);
        if (matcher.find()) {
            String tableName = matcher.group(1);
            String comment = matcher.group(2).trim();
            // 处理没有注释的情况
            if (comment.isEmpty()) comment = "无注释";
            result.add(new TableInfo(tableName, comment));
        }
    }
}
